package com.luoying.itew.util;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luoying.itew.R;
import com.luoying.itew.dialog.UpdataDialog;

/**
 * Created by Administrator on 2018/7/4.
 * time 2018-5-10
 * author zm
 */
public class DialogUtils {
    private static final String TAG = "DialogUtils";

    /**
     * 弹出Dialog去确认是否更新
     */
    public static void showUpdateDialog(final Context context, final String appVersionDownload, boolean b) {
        LogUtils.d(TAG,"downUrl == " + appVersionDownload);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_wallet_loading, null);
        final Dialog dialog = new Dialog(context, R.style.ConfirmFrameWindowStyle);
        dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        Window window = dialog.getWindow();

        WindowManager.LayoutParams wl = window.getAttributes();
        wl.gravity = Gravity.CENTER;
        // 以下这两句是为了保证按钮可以水平满屏
        /*wl.width = (int) getResources().getDimension(R.dimen.x560);
        wl.height = (int) getResources().getDimension(R.dimen.y250);//ViewGroup.LayoutParams.WRAP_CONTENT*/
        dialog.onWindowAttributesChanged(wl);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        RelativeLayout cancel = (RelativeLayout) view.findViewById(R.id.rl_dialog_wallet_cancel);
        RelativeLayout go = (RelativeLayout) view.findViewById(R.id.rl_dialog_wallet_go);
        TextView right = (TextView) view.findViewById(R.id.tv_wallet_go);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_dialog_setting_cancel);
        TextView title = (TextView) view.findViewById(R.id.tv_wallet_text);

        title.setText(context.getString(R.string.dialog_must_update_title));
        right.setText(context.getString(R.string.dialog_settings_break_ok));
        tvCancel.setText(context.getString(R.string.dialog_settings_break_cancel));
        if (b){
            cancel.setVisibility(View.GONE);
        }else {
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }


        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpdataDialog updataDialog = new UpdataDialog(context);
                updataDialog.showNoticeDialog(appVersionDownload);
                if (dialog!=null){
                    dialog.dismiss();
                }
                /*MyUtils.launchAppDetail("doudou.itheima.com.doudou", "", SettingsActivity.this);
                dialog.dismiss();*/
            }
        });

    }
}
